/**
 * 
 */
package us.gaaoc.framework.webapp.backingbeans.filingSearch;

import us.gaaoc.framework.businessmodel.Filing;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.apache.log4j.Logger;

import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;

/**
 * @author Frank 
 * August 7, 2007 
 * GAJE_BibbCounty_2.0
 */
@SuppressWarnings("unchecked")
public class SearchByPerson extends BaseBackingBean {
	
	/**
	 * property person's name
	 */
	
	/** Generated Serial Version UID */
	private static final long serialVersionUID = -3348446125476892815L;
	
	private Filing filing;
	private String firstName;	
	private String middleName;	
	private String lastName;		
	private boolean errorMessage;	
	private boolean displayFilings;
	private UIParameter linkToCaseDetails;
	private List<Filing> searchResultList;

	/**
	 * property dataModel
	 */
	 
	private DataModel dataModel;
	
	Logger LOG  = Logger.getLogger(SearchResults.class);

	private String courtUuid;
	

	/**
	 * Constructor
	 */
	public SearchByPerson() {
		courtUuid = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("courtUuid");
		dataModel = new ListDataModel();
	}

	/**
	 * @return the errorMessage
	 */
	public boolean isErrorMessage() {
		if(this.getSearchResultList().isEmpty()) {
			this.setErrorMessage(false);
		}
		else {
			this.setErrorMessage(true);
		}
		return errorMessage;
	}

	/**
	 * @param errorMessage the errorMessage to set
	 */
	public void setErrorMessage(boolean errorMessage) {
		this.errorMessage = errorMessage;
	}	
	/**
	 * @return Returns the dataModel.
	 */
	public DataModel getDataModel() {
		dataModel.setWrappedData(searchResultList); 	
		return dataModel;
	}

	/**
	 * @param dataModel
	 *            The dataModel to set.
	 */
	public void setDataModel(DataModel dataModel) {
		this.dataModel = dataModel;
	}
	
	/**
	 * @return the searchResultList
	 */
	public List getSearchResultList() {
		return this.searchResultList = this.getModelFacade().findCaseFilingsByPerson(this.firstName, this.middleName, this.lastName, courtUuid);		
	}

	/**
	 * @param searchResultList the searchResultList to set
	 */
	public void setSearchResultList(List searchResultList) {
		this.searchResultList = searchResultList;
	}


	/**
	 * This method loads the data from the database
	 * @return list
	 * @throws Exception
	 */
	public List loadSearchData() throws Exception {
		List<Filing> results = new ArrayList<Filing>();
		try {
			results = this.getModelFacade().findCaseFilingsByPerson(this.firstName, this.middleName, this.lastName, courtUuid);
			displayFilings = true;
		} catch (Exception e) {
			throw e;
		}
		return results;
	}
	
	public String gotoCaseDetailPage() {
		System.out.println("Inside the gotoCaseDetails");
		LOG.info("Inside the gotoDetailsPage...");
		LOG.info("Getting the case record..");
		Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();					
		Filing filing =(Filing)this.linkToCaseDetails.getValue();
		if(filing != null) {
			this.setFiling(filing);
		}		
	 	//String toString = "gotoCaseDetailsPage";	 		 	
		sessionMap.put("caseUuid", filing.getUuid());
	 	sessionMap.put("from", "searchPage");
	 	//sessionMap.put("from", "allFilingsTablesPage");
	 	//sessionMap.put("from", "establishmentFilingPage");
	 	return "gotoCaseDetailsPage";
	}
	
	public String clear() {
		this.filing = (Filing) this.getLinkToCaseDetails().getValue();
		this.getModelFacade().updateCaseRecordForClearCase(
				this.getFiling().getUuid(), true, courtUuid);
		//HibernatePhaseListener.getSessionFactory().getCurrentSession()
			//	.getTransaction().commit();
		return "stay";
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public boolean isDisplayFilings() {
		return displayFilings;
	}

	public void setDisplayFilings(boolean displayFilings) {
		this.displayFilings = displayFilings;
	}

	public UIParameter getLinkToCaseDetails() {
		return linkToCaseDetails;
	}

	public void setLinkToCaseDetails(UIParameter linkToCaseDetails) {
		this.linkToCaseDetails = linkToCaseDetails;
	}

	public Filing getFiling() {
		return filing;
	}

	public void setFiling(Filing filing) {
		this.filing = filing;
	}


	
}
